import dayjs from 'dayjs';


export const PERIOD_INTERVAL_MAP = {
  '1m': 1 * 60 * 1000,
  '5m': 5 * 60 * 1000,
  '15m': 15 * 60 * 1000,
  '30m': 30 * 60 * 1000,
  '1h': 60 * 60 * 1000,
  '4h': 4 * 60 * 60 * 1000,
  '1d': 24 * 60 * 60 * 1000,
}
/**
 * 获取一段时间
 * @example getInterval('1d', 7)
 */
export function getInterval(period: keyof typeof PERIOD_INTERVAL_MAP, num: number) {
  const interval = PERIOD_INTERVAL_MAP[period];
  return [
    dayjs(Date.now() - (interval * num)),
    dayjs()
  ]
}

/**
 * 获取周期时间
 */
export function roundTimeToInterval(period: keyof typeof PERIOD_INTERVAL_MAP, time: number) {
  return Math.floor(time / PERIOD_INTERVAL_MAP[period]) * PERIOD_INTERVAL_MAP[period];
}

/**
 * 按天分割时间段
 * @param start 
 * @param end 
 * @param day 分割的天数
 */
export function getDayRange(start: Date, end: Date, day = 1): [Date, Date][] {
  const intervals: [Date, Date][] = [];
  let startTime = start;

  while (startTime < end) {
    // 分割时间段
    const nextDay = dayjs(startTime).add(day, 'day').toDate();
    // 如果下一天小于结束时间，那么结束时间就是下一天，否则就是结束时间
    const endTime = nextDay <= end ? nextDay : end;
    intervals.push([startTime, endTime]);
    startTime = endTime;
  }

  return intervals;
}


export function isCurrentTimeOnTheHour(): boolean {
  const currentDate = new Date();
  const currentMinutes = currentDate.getMinutes();
  const currentSeconds = currentDate.getSeconds();

  return currentMinutes === 0 && currentSeconds === 0;
}